package com.xbs.dao.user;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xbs.dao.conn.BaseDao;
import com.xbs.dao.conn.ParamSet;
import com.xbs.dao.conn.RowDatas;
import com.xbs.entity.MyCenterEntity;

/**
 * 个人中心订单Dao类
 * 1. 查询短租自驾订单信息
 * 2. 查询顺风车订单信息
 * @author Administrator
 *
 */
public class MyCenterDao extends BaseDao{
	
	private RowDatas<MyCenterEntity> mycenter;   //声明一个车对象结果处理对象
	
	public MyCenterDao(){
		mycenter = new mycenterDate(); //实例结果处理对象
	}
	
	/**
	 * 1. 查询短租自驾订单信息
	 * @return
	 */
	public List<MyCenterEntity> getDuan(String name, String status, Date date1, Date date2){
		StringBuffer sb = new StringBuffer("select *,st2.store_city as store_city2, st2.store_name as store_name2 from tb_common_order"
				+ " left join tb_car_model on tb_common_order.co_car_type = tb_car_model.car_model_id "
				+ " left join tb_store st1 on st1.store_id = tb_common_order.co_get_store "
				+ " left join tb_store st2 on st2.store_id = tb_common_order.co_return_store "
				+ " left join tb_time  on tb_common_order.co_id = tb_time.time_order_id "
				+ " left join tb_order_money on tb_common_order.co_id = tb_order_money.order_id "
				+ " left join tb_rent on tb_common_order.co_id = tb_rent.co_id "
				+ " left join tb_user on tb_rent.user_idcard = tb_user.user_idcard "
				+ " where co_order_kind = '自驾租车' ");
		if(!StringUtils.isEmpty(name)){
			sb.append(" and tb_user.user_name ='"+name+"'");
		}				
		if(!StringUtils.isEmpty(status)){
			sb.append(" and co_order_status ='"+ status+"'");
		}				
		if(!StringUtils.isEmpty(date1,date2)){
			sb.append(" and time_plan_get between '"+ date1+"' and '"+ date2+"'");
		}
		String sql = sb.toString();
		return queryList(sql,mycenter);
	}
	
	/**
	 * 2. 查询顺风车订单信息
	 * @return
	 */
	public List<MyCenterEntity> getShun(String name, String status, Date date1, Date date2){
		StringBuffer sb = new StringBuffer("select *,st2.store_city as store_city2, st2.store_name as store_name2 from tb_common_order"
				+ " left join tb_car_model on tb_common_order.co_car_type = tb_car_model.car_model_id "
				+ " left join tb_store st1 on st1.store_id = tb_common_order.co_get_store "
				+ " left join tb_store st2 on st2.store_id = tb_common_order.co_return_store "
				+ " left join tb_time  on tb_common_order.co_id = tb_time.time_order_id "
				+ " left join tb_order_money on tb_common_order.co_id = tb_order_money.order_id "
				+ " left join tb_rent on tb_common_order.co_id = tb_rent.co_id "
				+ " left join tb_user on tb_rent.user_idcard = tb_user.user_idcard "
				+ " where co_order_kind = '顺风车'");
		if(!StringUtils.isEmpty(name)){
			sb.append(" and tb_user.user_name ='"+name+"'");
		}				
		if(!StringUtils.isEmpty(status)){
			sb.append(" and co_order_status ='"+ status+"'");
		}				
		if(!StringUtils.isEmpty(date1,date2)){
			sb.append(" and time_plan_get between '"+ date1+"' and '"+ date2+"'");
		}
		String sql = sb.toString();
		return queryList(sql,mycenter);
	}
	
	/**
	 * 3. 根据订单号修改订单信息
	 * @return
	 */
	public int update(double money, String status, String coid){
		String sql = "update tb_common_order set co_fact_pay_money = ?, co_order_status = ?  where co_id = ?";
		return updateExecuted(sql, new ParamSet(money, status, coid));
	}
	
	
	/**
	 * 通过内部类实现结果处理接口
	 * @author Administrator
	 *
	 */
	private class mycenterDate implements RowDatas<MyCenterEntity>{

		@Override
		public MyCenterEntity getRowData(ResultSet rs) throws SQLException {
			MyCenterEntity mycenter = new MyCenterEntity();
			//给MyCenterEntity赋值
			mycenter.setUser_name(rs.getString("user_name"));
			mycenter.setCo_id(rs.getString("co_id"));
			mycenter.setCo_order_status(rs.getString("co_order_status"));
			mycenter.setCo_plan_pay_money(rs.getDouble("co_plan_pay_money"));
			mycenter.setCo_fact_pay_money(rs.getDouble("co_fact_pay_money"));
			mycenter.setTime_plan_get(rs.getDate("time_plan_get"));
			mycenter.setTime_fact_get(rs.getDate("time_fact_get"));
			mycenter.setTime_plan_return(rs.getDate("time_plan_return"));
			mycenter.setTime_fact_return(rs.getDate("time_fact_return"));
			mycenter.setStore_city(rs.getString("store_city"));
			mycenter.setStore_name(rs.getString("store_name"));
			mycenter.setStore_city2(rs.getString("store_city2"));
			mycenter.setStore_name2(rs.getString("store_name2"));
			mycenter.setCar_model_name(rs.getString("car_model_name"));
			mycenter.setCar_model_seating(rs.getInt("car_model_seating"));
			mycenter.setCar_model_image(rs.getString("car_model_image"));
			return mycenter;
		}
		
	}
}
